﻿//----------------------------------------------------------------------------------------------------
// The The Level Editor State of the game
//----------------------------------------------------------------------------------------------------

package HG.GameStates
{
	import flash.events.*;
	import flash.geom.Point;
	
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent;
	
	import HG.Debug;
	import HG.GameService;
	import HG.Managers.StateManager;
	import HG.Managers.NodeManager;
	import HG.Managers.LevelManager;
	import HG.Managers.TurnManager;
	import HG.Managers.ActionManager;
	
	import HG.Visual.VisualNode;
	import HG.Visual.VisualConnection;
	import HG.Visual.RadialMenu;
	import HG.Actions.ActionHack;
	import HG.Managers.PlayerManager;
	import HG.Managers.ResourceManager;
	import HG.Managers.GameRuleManager;
	import HG.Managers.CameraManager;
	
	import HG.Events.GameEvent;
	import HG.Events.ResourceEvent;
	import HG.Events.LevelEvent;
	import HG.Events.TurnEvent;
	

	public class PlayState extends IGameState
	{
		var m_stateManager:StateManager;
		var m_nodeManager:NodeManager;
		var m_levelManager:LevelManager;
		var m_actionManager:ActionManager;
		var m_turnManager:TurnManager;
		var m_playerManager:PlayerManager;
		var m_resourceManager:ResourceManager;
		var m_gameRuleManager:GameRuleManager;
		var m_cameraManager:CameraManager;
		
		var m_optionsOpen:Boolean;
		
		var m_menu:RadialMenu;
		
		//----------------------------------------------------------------------------------------------------
		public function PlayState()
		{
			m_stateName = "PlayState";
		}
		
		//----------------------------------------------------------------------------------------------------
		public override function Begin()
		{
			super.Begin();
			
			m_stateManager = GameService.GetInstance().GetStateManager();
			m_nodeManager = GameService.GetInstance().GetNodeManager();
			m_levelManager = GameService.GetInstance().GetLevelManager();
			m_turnManager = GameService.GetInstance().GetTurnManager();
			m_playerManager = GameService.GetInstance().GetPlayerManager();
			m_resourceManager = GameService.GetInstance().GetResourceManager();
			m_gameRuleManager = GameService.GetInstance().GetGameRuleManager();
			m_cameraManager = GameService.GetInstance().GetCameraManager();
			
			m_turnManager.addEventListener(TurnEvent.BEGIN, OnTurnBegin);
			
			Debug.ASSERT(m_movie, "No Play Clip");
			Debug.ASSERT(m_movie.Content, "No Play Content");
			
			// subscribe manager events
			m_gameRuleManager.addEventListener(GameEvent.END, OnGameEnd);
			
			// Options menu
			m_movie.Content.Options.Button_Gear.addEventListener(MouseEvent.CLICK, Button_Gear);
			m_movie.Content.Options.Content.Button_Back.addEventListener(MouseEvent.CLICK, Button_Back);
			
			// Execute
			m_movie.Content.Button_Execute.alpha = 0;
			m_movie.Content.Button_Execute.addEventListener(MouseEvent.CLICK, Button_Execute);
			
			//Ensure options menu is closed
			m_optionsOpen = false;
			m_movie.Content.Options.gotoAndStop('hide');
			
			// Radial Menu
			m_menu = new RadialMenu();
			m_movie.Content.LevelCanvas.LevelHolder.addChild(m_menu);
			
			m_levelManager.SetVisualLevelContainer(m_movie.Content.LevelCanvas.LevelHolder);
			m_levelManager.addEventListener(LevelEvent.LOAD, OnLevelLoaded);
			m_movie.Content.LevelCanvas.LevelHolder.SetEditorMode(false);
			
			m_levelManager.LoadLevel(0);
			
			m_cameraManager.SetCanvas(m_movie.Content.LevelCanvas);
			
			// Add environment
			m_playerManager.AddPlayer(PlayerManager.PLAYER_ENVIRONMENT, 0);
			//var ID:Number = m_playerManager.AddPlayer(PlayerManager.PLAYER_HUMAN);
			//m_playerManager.SetCurrentPlayerID(ID);
		}
		
		//----------------------------------------------------------------------------------------------------
		public override function End()
		{1
			super.End();
		}
		
		//----------------------------------------------------------------------------------------------------
		public override function OnTick(DetaTime:Number)
		{
		}
		
		//----------------------------------------------------------------------------------------------------
		// Events
		//----------------------------------------------------------------------------------------------------
		public function OnGameEnd(evt:GameEvent)
		{
			m_stateManager.SwitchtoState(StateManager.STATE_POSTGAME);
		}
		
		//----------------------------------------------------------------------------------------------------
		public function OnLevelLoaded(evt:LevelEvent)
		{
			//Turn
			//m_turnManager.BeginTurn();
			
			//Nodes
			var nodes:Array = m_movie.Content.LevelCanvas.LevelHolder.GetVisualNodes();
			
			var index:Number = 0;
			var listLength:Number = nodes.length;
			
			for (index = 0; index < listLength; ++index)
			{
				nodes[index].addEventListener(MouseEvent.MOUSE_DOWN, OnNodePressed);
			}
		}
		
		//----------------------------------------------------------------------------------------------------
		private function ShowOptionsMenu(show:Boolean)
		{
			if (show == m_optionsOpen)
				return;
			
			if (show)
			{
				m_optionsOpen = true;
				m_movie.Content.Options.gotoAndPlay('in');
			}
			else
			{
				m_optionsOpen = false;
				m_movie.Content.Options.gotoAndPlay('out');
			}
		}
		
		//----------------------------------------------------------------------------------------------------
		// Button Callbacks
		//----------------------------------------------------------------------------------------------------
		private function Button_Execute(me:MouseEvent):void
		{
			m_turnManager.PlayerInputComplete(m_playerManager.GetCurrentPlayerID());
			new Tween(m_movie.Content.Button_Execute, "alpha", None.easeNone, m_movie.Content.Button_Execute.alpha, 0, 0.25, true);
			m_menu.SetEnabled(false);
		}
		
		//----------------------------------------------------------------------------------------------------
		private function Button_Back(me:MouseEvent):void
		{
			m_stateManager.SwitchtoState(StateManager.STATE_MENU);
		}
		
		//----------------------------------------------------------------------------------------------------
		private function Button_Gear(me:MouseEvent):void
		{
			if (!m_optionsOpen)
			{
				ShowOptionsMenu(true);
			}
			else
			{
				ShowOptionsMenu(false);
			}
		}
		
		//----------------------------------------------------------------------------------------------------
		// Game Callbacks
		//----------------------------------------------------------------------------------------------------
		private function OnNodePressed(me:MouseEvent):void
		{
			var node:VisualNode = VisualNode(me.currentTarget);
			m_menu.SetTarget(node);
		}
		
		//----------------------------------------------------------------------------------------------------
		private function OnTurnBegin(evt:TurnEvent)
		{
			if (evt.m_inputPlayerID == m_playerManager.GetCurrentPlayerID())
			{
				m_menu.SetEnabled(true);
				new Tween(m_movie.Content.Button_Execute, "alpha", None.easeNone, m_movie.Content.Button_Execute.alpha, 1, 0.25, true);
				//m_movie.Content.Button_Execute.visible = true;
			}
			else
			{
				m_menu.SetEnabled(false);
				new Tween(m_movie.Content.Button_Execute, "alpha", None.easeNone, m_movie.Content.Button_Execute.alpha, 0, 0.25, true);
				//m_movie.Content.Button_Execute.visible = false;
			}
		}
	}

}











